<?php
require 'Home/Model/Model.php';
class HomeModel extends Model{
    function __Construct(){
        parent::__Construct();
    }
    function getHotlist(){
        $sql = 'select * from videos order by hittimes desc limit 4';//从第0行开始只显示8行
        $hotList = $this->conn->query($sql);
        return $hotList;
    }
    function getRanklist(){
        $sql = 'select * from videos order by downtimes desc limit 4';//从第0行开始只显示8行
        $rankList = $this->conn->query($sql);
        return $rankList;
    }
    function getAllVideos(){
        // $vtypes = array('电影','电视剧');
        /*$vtypes = array(
            array('tid'=>1,'tname'=>'电影'),
            array('tid'=>2,'tname'=>'电视剧')
				);*/
            $videotypes = $this->getVTypes();   //数据库所有的视频类别的信息都在$videotypes里面了。
//        var_dump($videotypes);
			$videos = array();
			foreach($videotypes as $vtype){
                $sql = "select * from videos where tid ={$vtype['tid']}  order by vid desc limit 0,6";//从第0行开始只显示8行
//                var_dump($sql);
                $v = $this->conn->query($sql);
//                var_dump($v);
                $vv['tid']=$vtype['tid'];
				$vv['typename']=$vtype['typename'];
				$vv['list']=$v;
				$videos[]=$vv;
//                var_dump($videos);

			}
			return $videos;

		}

    function getVTypes(){
        $sql = 'select * from videotype';
        $videoTypes = $this->conn->query($sql);
//        var_dump($videoTypes);
        return $videoTypes;

    }

    function getOneTypeList($tid){
        $sql = "select * from videos where tid = {$tid}";
        $result = $this->conn->query($sql);
        return $result;
    }

    function getTypeByID($tid){
        $sql = "select * from videotype where tid = {$tid}";
        $result = $this->conn->query($sql);
        return $result;
    }

    function getDetailByID($vid){
        $sql = "select * from videos join videotype on videos.tid=videotype.tid join admins on admins.adminid = videos.uploadadmin where vid = {$vid}";
        $result = $this->conn->query($sql);
        return $result;
    }

    function getCommentByID($vid){
        $sql = "select content from comments join videos on comments.vid = videos.vid where vid = {$vid}";
        $result = $this->conn->query($sql);
        return $result;
    }

    function getScore($vid){
        $sql = "select avg(score) from levels where vid={$vid}";
        $result = $this->conn->query($sql);
        $result = $result['avg(score)'];
        $p= stripos($result, '.');  //保留两位小数
        $result = substr($result,0,$p+2);
        return $result;
    }

}
?>